<?php
/* 

选择列表控件用法

<html:listselect class="btn btn-success" name="customer_id" multiple="1" url="{:U('Intesys/Customer/select')}" selected="info['id']" title="选择客户" id="customer_id" />

客户管理模块
用于积分系统项目管理子系统
 */
class CustomerAction extends IntesysAction {
	//初始化
	public function _initialize(){
		parent::_initialize();
		C('TOKEN_ON', false);
		$_POST['site_id'] = __SITEID__;
		$this->dao = D('Customer');
	}
	//客户列表
	public function index(){
		$this->pagesize = 8;
		$this->listmap = "site_id = ". __SITEID__;
		
		if($this->_param('keywords')){
			$keywords=$this->_param('keywords');
			$_GET['keywords']=urlencode($this->_param('keywords'));
			$this->listmap.=" and ( title like '%{$keywords}%' or tel like '%{$keywords}%' or address like '%{$keywords}%' or remark like '%{$keywords}%' or contacts like '%{$keywords}%' or weixin like '%{$keywords}%' or QQ like '%{$keywords}%' or mobile like '%{$keywords}%')";
			$this->keywords=$keywords;
		}

		if($this->_param('pagesize')){
			$this->pagesize=$this->_param('pagesize');
			$this->assign('pagesize',$this->pagesize);
		}

        if($this->_param('id')){
            $id=$this->_param('id');
            $this->listmap.= " and ( customercategory_id = '@".$id."@' or customercategory_id like '%@".$id."@%') ";
        }

		if($this->_param('category')&&$this->_param('category')!='99999999'){

			$this->listmap.=" and ( customercategory_id = ".$this->_param('category')." or customercategory_id like '%@".$this->_param('category')."@%') ";
			$this->category=$this->_param('category');
		}

		$category=D('customercategory')->where('site_id = '.__SITEID__.' and status = 1')->select();
		$this->assign('candy',$category);

        $cate=D('customercategory')->where('site_id = '.__SITEID__.' and status = 1')->select();
        $this->assign('holy',$cate);
		parent::_list();
		$db = M('Customer_log');
		$data = $this->get('list');
		foreach($data as $k => $v){
			$log =  $db -> where(array('customer_id' => $v['id'])) -> order('id desc') -> getField('content');
			$data[$k]['lastlog'] = $log;
		}
		$this->assign('list', $data);
		
		$this->display();
	}

    public function add(){
        $category=D('customercategory')->select();
        $this->assign('candy',$category);
        $this->display();
    }
	public function insert(){
        $data['title']=$this->_param('title');
        $data['contacts']=$this->_param('contacts');
        $data['mobile']=$this->_param('mobile');
        $data["tel"]=$this->_param("tel");
        $data["QQ"]=$this->_param("QQ");
        $data["weixin"]=$this->_param("weixin");
        $data["address"]=$this->_param("address");
        $cus=implode('',$this->_param('customercategory_id'));
        $data['customercategory_id']=$cus;
        $set=D('customer')->add($data);
        if($set){
            $this->success('插入成功',U('Customer/index'));
        }else{
            $this->error('插入失败',U('Customer/index'));
        }
    }
    public function edit(){
        $category=D('customercategory')->select();
        $this->assign('candy',$category);

        $id=$this->_param('id');
        $list=D('customer')->where('id = '.$id)->find();
        $this->assign('info',$list);
        $this->display();
    }
    public function update(){
        $data['id']=$this->_param('id');
        $data['title']=$this->_param('title');
        $data['contacts']=$this->_param('contacts');
        $data['mobile']=$this->_param('mobile');
        $data["tel"]=$this->_param("tel");
        $data["QQ"]=$this->_param("QQ");
        $data["weixin"]=$this->_param("weixin");
        $data["address"]=$this->_param("address");
        $cus=implode('',$this->_param('customercategory_id'));
        $data['customercategory_id']=$cus;
        $set=D('customer')->where('id = '.$data['id'])->save($data);
        if($set){
            $this->success('编辑成功',U('Customer/index'));
        }else{
            $this->error('编辑失败',U('Customer/index'));
        }
    }
	//选择列表
	public function select(){

		
		$this->pagesize = 8;
		
		$this->listmap = "site_id = ". __SITEID__;
		
		if($this->_get('selected') && $this->_get('selected') != ''){
			$this->pagesize = 100;
			$this->listmap = "id in(".$this->_get('selected').")";
		}
		
		if($this->_param('keywords')){
			$keywords=$this->_param('keywords');
			$_GET['keywords']=urlencode($this->_param('keywords'));
			$this->listmap.=" and title like '%{$keywords}%'";
			$this->keywords=$keywords;
		}
		
		parent::_list();
		$db = M('Customer_log');
		$data = $this->get('list');
		foreach($data as $k => $v){
			$log =  $db -> where(array('customer_id' => $v['id'])) -> order('id desc') -> getField('content');
			$data[$k]['lastlog'] = $log;
		}
		$this->assign('list', $data);
		
		$this->display();
	}
	

	//显示日志 
	public function addlog(){
		$db = M('Customer_log');
		$this->customer_id = $this->_get('id');
		$data = $db -> where(array('customer_id' => $this->customer_id)) -> order('id desc') -> select();

		
		$this->assign('list', $data);
		$this->display();
	}
	//修改日志 
	public function editlog(){
		$db = M('Customer_log');
		$this->customer_id = $this->_get('id');
		$this->info = $db -> where(array('id' => $this->_get('id'))) -> find();
		$data = $db -> where(array('customer_id' => $this->customer_id)) -> order('id desc') -> select();
		$this->assign('list', $data);
		$this->display();
	}
	
	//写入日志 
	public function insertlog(){
		$this->dao = M('Customer_log');
		$_POST['dateline'] = time();
		$_POST['nickname'] = $this->userInfo['nickname'];
		$_POST['member_id'] = $this->userInfo['id'];
		 
		parent::insert();
	}
	public function search(){

		$category=D('customercategory')->select();
		$this->assign('candy',$category);
		$this->display();
	}

	//APP 获取一个信息
	public function getInfo(){
		if($this->_post('customer_id')&&$this->_post('requestType')==111){
			$map='customer_id='.$this->_post('customer_id').' and site_id='.__SITEID__;
			$info=$this->dao->where($map)->Field(['id'=>'customer_id','title'=>'company','name', 'mobile'=>'phone','QQ','Email','address','remark','address'])->find();
			$status=$info?1:0;
			die(json_encode(['status'=>$status,'data'=>$info]));
		}
	}
	//APP 获取多个信息
	public function getInfos(){
		if($this->_post('requestType')==110){
			$map=' site_id='.__SITEID__;
			$info=$this->dao->where($map)->Field(['id'=>'customer_id','title'=>'company','contacts', 'mobile'=>'phone','QQ','Email','address','remark','address'])->select();
			$status=$info?1:0;
			die(json_encode(['status'=>$status,'data'=>$info]));
		}
	}
}